package com.diandian.backend.dingding.data.service;

import com.diandian.backend.dingding.data.domain.DingdingStaffSalaryOpenApi;
import com.diandian.backend.dingding.data.dto.DingdingDeptTreeDetailParamDTO;
import com.diandian.backend.dingding.data.mapper.DingdingStaffSalaryOpenApiMapper;
import com.diandian.backend.dingding.data.param.DingdingStaffSalaryPageOpenApiParam;
import com.diandian.backend.dingding.data.param.DingdingStaffSalaryStaffPageOpenApiParam;
import com.diandian.backend.dingding.data.resp.DingdingDeptTreeDetailResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class DingdingStaffSalaryOpenApiService {

    @Autowired
    private DingdingDeptTreeService dingdingDeptTreeService;

    @Autowired
    private DingdingStaffSalaryOpenApiMapper dingdingStaffSalaryOpenApiMapper;

    public PageResult page(DingdingStaffSalaryPageOpenApiParam data) {

        data.check();

        int pageNum = data.getPageNum();
        int pageSize = data.getPageSize();
        String belong = data.getBelong();

        String queryUserChoiceDeptCode = data.getQueryUserChoiceDeptCode();
        if(queryUserChoiceDeptCode != null && !"".equals(queryUserChoiceDeptCode)) {
            DingdingDeptTreeDetailParamDTO dingdingDeptTreeDetailParamDTO = new DingdingDeptTreeDetailParamDTO();
            dingdingDeptTreeDetailParamDTO.setBelong(belong);
            dingdingDeptTreeDetailParamDTO.setDeptCode(queryUserChoiceDeptCode);
            DingdingDeptTreeDetailResp deptTreeDetailResp = dingdingDeptTreeService.getDetail(dingdingDeptTreeDetailParamDTO);
            if(deptTreeDetailResp == null) {
                throw new DefaultException("部门编码无效");
            }
            String deptPathPrefix = deptTreeDetailResp.getDeptPath();
            data.setDeptPathPrefix(deptPathPrefix);
        }
        int totalCount = dingdingStaffSalaryOpenApiMapper.getCount(data);
        List<DingdingStaffSalaryOpenApi> dataList = dingdingStaffSalaryOpenApiMapper.getPage(data, data.getPageSize(), (data.getPageNum() - 1) * data.getPageSize());

        if(dataList != null) {
            dataList.forEach(x -> {
                String name = x.getName();
                if(name == null || "".equals(name)) {
                    x.setName("信息缺失");
                }
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, dataList);
    }

    public PageResult pageStaff(DingdingStaffSalaryStaffPageOpenApiParam data) {

        data.check();

        int pageNum = data.getPageNum();
        int pageSize = data.getPageSize();

        DingdingStaffSalaryPageOpenApiParam pageParam = new DingdingStaffSalaryPageOpenApiParam();
        pageParam.setBelong(data.getBelong());
        pageParam.setPageNum(pageNum);
        pageParam.setPageSize(pageSize);
        pageParam.setJobNumber(data.getQueryUserJobNumber());

        int totalCount = dingdingStaffSalaryOpenApiMapper.getCount(pageParam);
        List<DingdingStaffSalaryOpenApi> dataList = dingdingStaffSalaryOpenApiMapper.getPage(pageParam, data.getPageSize(), (data.getPageNum() - 1) * data.getPageSize());

        if(dataList != null) {
            dataList.forEach(x -> {
                String name = x.getName();
                if(name == null || "".equals(name)) {
                    x.setName("信息缺失");
                }
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, dataList);
    }

}
